/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "GesturePage.h"
#include "onyx/ui/message_dialog.h"

extern CoListItems g_mainOperations;

CoListItems g_dragDirections;

GesturePage::GesturePage(CoGestureMap& gestureMap, QWidget* parent)
: CoTabPage(parent)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_gestureMap = &gestureMap;

    g_dragDirections.clear();
    g_dragDirections.append(CoListItem(tr("单击"), DragNone));
    g_dragDirections.append(CoListItem(tr("↑"), DragUp));
    g_dragDirections.append(CoListItem(tr("↓"), DragDown));
    g_dragDirections.append(CoListItem(tr("←"), DragLeft));
    g_dragDirections.append(CoListItem(tr("→"), DragRight));

    QHBoxLayout* layoutNew = new QHBoxLayout(this);
    m_bNew = new ui::OnyxPushButton(tr("新建"), this);
    layoutNew->addWidget(m_bNew);
    layoutNew->addStretch(1);
    m_layout.addLayout(layoutNew);
    m_layout.addStretch(1);

    connect(m_bNew, SIGNAL(clicked()), this, SLOT(newColumn()));

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void GesturePage::initByGestureMap()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    for (int i = 0; i < m_viewColumns.count(); i++)
    {
        m_viewColumns[i]->close();
    }
    m_viewColumns.clear();

    QList<int> keys = m_gestureMap->keys();
    for (int i = 0; i < keys.count(); i++)
    {
        GestureViewWidget* viewColumn = new GestureViewWidget(keys[i], &g_mainOperations[(*m_gestureMap)[keys[i]]], this);
        m_viewColumns.append(viewColumn);
        m_layout.insertWidget(m_layout.count() - 2, viewColumn);

        connect(viewColumn, SIGNAL(deleted(GestureViewWidget*)), this, SLOT(deleteColumn(GestureViewWidget*)));
    }

    if (!m_viewColumns.isEmpty())
    {
        setTabOrder(m_viewColumns.last(), m_bNew);
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void GesturePage::deleteColumn(GestureViewWidget* column)
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_layout.removeWidget(column);
    m_viewColumns.remove(m_viewColumns.indexOf(column));
    column->close();

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void GesturePage::newColumn()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    GestureViewWidget* viewColumn = new GestureViewWidget(0, &g_mainOperations[0], this);
    connect(viewColumn, SIGNAL(deleted(GestureViewWidget*)), this, SLOT(deleteColumn(GestureViewWidget*)));
    m_viewColumns.append(viewColumn);
    m_layout.insertWidget(m_layout.count() - 2, viewColumn);
    setTabOrder(viewColumn, m_bNew);

    Out_Log(__FUNCTION__, COCLASSNAME);
}

void GesturePage::writeToGestureMap()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    m_gestureMap->clear();

    for (int i = 0; i < m_viewColumns.count(); i++)
    {
        (*m_gestureMap)[m_viewColumns[i]->m_gesture] = m_viewColumns[i]->m_operItem->id;
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
}

bool GesturePage::isConflict()
{
    In_Log(__FUNCTION__, COCLASSNAME);

    for (int i = 0; i < m_viewColumns.count() - 1; i++)
    {
        for (int j = i+1; j < m_viewColumns.count(); j++)
        {
            if (m_viewColumns[i]->m_gesture == m_viewColumns[j]->m_gesture)
            {
                ui::MessageDialog dlg(QMessageBox::Warning,
                                      tr("错误"),
                                      tr("存在冲突的手势\"") + m_viewColumns[i]->gestureString() + tr("\""),
                                      QMessageBox::Ok);
                dlg.exec();
                update();

                Out_Log(__FUNCTION__, COCLASSNAME);
                return true; 
            }
        }
    }

    Out_Log(__FUNCTION__, COCLASSNAME);
    return false;
}
